package org.bob.web.controller;


import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
import co.elastic.clients.elasticsearch._types.query_dsl.Operator;
import co.elastic.clients.elasticsearch.core.SearchRequest;
import co.elastic.clients.elasticsearch.core.SearchResponse;
import lombok.extern.slf4j.Slf4j;
import org.bob.web.model.SearchReq;
import org.bob.web.model.SearchRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

@Slf4j
@RestController
public class MatchController extends BaseController {

    @Autowired
    private ElasticsearchClient elasticsearchClient;


    @GetMapping("/match")
    public Mono<SearchRes> match(){
        SearchRequest searchRequest = SearchRequest.of(s->s.index("hotel").query(q->q.match(m->m.field("title").query("水哥"))));
        SearchResponse<Object> search;
        try {
            search = elasticsearchClient.search(searchRequest, Object.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toMono(search);
    }
    @GetMapping("/matchByPercent")
    public Mono<SearchRes> matchByPercent(){
        SearchRequest searchRequest = SearchRequest.of(s->s.index("hotel").query(q->q.match(m->m.field("title").query("哥酒店").minimumShouldMatch("2"))));
        SearchResponse<Object> search;
        try {
            search = elasticsearchClient.search(searchRequest, Object.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toMono(search);
    }

    @GetMapping("/matchByAnd")
    public Mono<SearchRes> matchByAnd(){
        SearchRequest searchRequest = SearchRequest.of(s->s.index("hotel").query(q->q.match(m->m.field("title").query("水哥").operator(Operator.And))));
        SearchResponse<Object> search;
        try {
            search = elasticsearchClient.search(searchRequest, Object.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toMono(search);
    }

    @GetMapping("/matchByMulti")
    public Mono<SearchRes> matchByMulti(){
        SearchRequest searchRequest = SearchRequest.of(s->s.index("hotel").query(q->q.multiMatch(m->m.fields("title","tag").query("水哥").operator(Operator.And))));
        SearchResponse<Object> search;
        try {
            search = elasticsearchClient.search(searchRequest, Object.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toMono(search);
    }

    @GetMapping("/matchByPhrase")
    public Mono<SearchRes> matchByPhrase(){
        SearchRequest searchRequest = SearchRequest.of(s->s.index("hotel").query(q-> q.matchPhrase(m -> m.field("title").query("水哥"))));
        SearchResponse<Object> search;
        try {
            search = elasticsearchClient.search(searchRequest, Object.class);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return toMono(search);
    }
}
